import {useState, useRef, useEffect} from 'react';

export const usePromiseLoading = (callback) => {
  const [loading, setLoading] = useState(false);

  const fn = (...arg) => {
    setLoading(true);
    return callback(...arg)
      .finally(() => {
        setLoading(false);
      });
  };
  return [fn, loading];
};

export const usePrevious = (value) => {
  const ref = useRef();
  useEffect(() => {
    ref.current = value;
  });
  return ref.current;
}
